package com.x.provider.customer.factory.dto.impl;

import com.x.core.utils.BeanUtil;
import com.x.core.utils.SpringUtils;
import com.x.provider.api.customer.enums.CustomerOptions;
import com.x.provider.api.customer.model.dto.customer.CustomerListReqDTO;
import com.x.provider.api.customer.model.dto.customer.CustomerDTO;
import com.x.provider.api.customer.model.dto.customer.CustomerSimpleDTO;
import com.x.provider.api.oss.service.OssRpcService;
import com.x.provider.customer.factory.dto.CustomerDTOBuilder;
import com.x.provider.customer.factory.dto.CustomerDTOFactory;
import com.x.provider.customer.service.customer.CustomerService;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author: liushenyi
 * @date: 2022/07/25/16:04
 */
@Service
public class CustomerFactoryImpl implements CustomerDTOFactory {

    private final CustomerService customerService;
    private final List<CustomerDTOBuilder> CUSTOMER_DTO_BUILDER = SpringUtils.getBanListOfType(CustomerDTOBuilder.class);
    private final OssRpcService ossRpcService;

    public CustomerFactoryImpl(CustomerService customerService,
                               OssRpcService ossRpcService){
        this.customerService = customerService;
        this.ossRpcService = ossRpcService;
    }

    @Override
    public Map<Long, CustomerDTO> prepare(CustomerListReqDTO listCustomerAO) {
        Map<Long, CustomerDTO> result = new HashMap<>(listCustomerAO.getCustomerIds().size());
        if (listCustomerAO.getCustomerOptions().contains(CustomerOptions.CUSTOMER.name())){
            final List<CustomerDTO> customerList = BeanUtil.prepare(customerService.listCustomer(listCustomerAO.getCustomerIds()), CustomerDTO.class);
            customerList.forEach(item -> {
                result.put(item.getId(), item);
            });
        }
        else{
            listCustomerAO.getCustomerIds().forEach(item -> {
                result.put(item, CustomerDTO.builder().id(item).build());
            });
        }
        CUSTOMER_DTO_BUILDER.forEach(item -> {
            item.build(listCustomerAO, result);
        });
        return result;
    }

    @Override
    public Map<Long, CustomerSimpleDTO> prepareSimpleCustomer(CustomerListReqDTO customerListReqDTO) {
        if (!customerListReqDTO.getCustomerOptions().contains(CustomerOptions.CUSTOMER.name())){
            customerListReqDTO.getCustomerOptions().add(CustomerOptions.CUSTOMER.name());
        }
        Map<Long, CustomerDTO> customerDTO = prepare(customerListReqDTO);
        Set<String> urlSet = customerDTO.values().stream().map(item -> item.getCustomerResumeList().get(0).getAvatarId()).collect(Collectors.toSet());
        Map<String, String> urlMap = ossRpcService.listObjectBrowseUrl(new ArrayList<>(urlSet)).getData();
        return customerDTO.entrySet().stream().collect(Collectors.toMap(item -> item.getKey(), item -> prepare(null, item.getValue(), urlMap)));
    }

    public CustomerSimpleDTO prepare(CustomerSimpleDTO dest, CustomerDTO src, Map<String, String> urlMap){
        if (dest == null){
            dest = new CustomerSimpleDTO();
        }
        dest.setId(src.getId());
        dest.setUserName(src.getUserName());
        dest.setStatistic(src.getStatistic());
        dest.setNickName(src.getCustomerResumeList().get(0).getNickName());
        dest.setCustomerRelation(src.getCustomerRelation());
        dest.setGender(src.getCustomerResumeList().get(0).getGender());
        dest.setAvatarUrl(urlMap.get(src.getCustomerResumeList().get(0).getAvatarId()));
        return dest;
    }



}
